/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return new RegExp("^(https?:|mailto:|tel:)").test(path);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ["admin", "editor"];
  return valid_map.indexOf(str.trim()) >= 0;
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg = new RegExp(
    "^(https?|ftp):\\/\\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\\.)*[a-zA-Z0-9-]+\\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$"
  );
  return reg.test(url);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = new RegExp("^[a-z]+$");
  return reg.test(str);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = new RegExp("^[A-Z]+$");
  return reg.test(str);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = new RegExp("^[A-Za-z]+$");
  return reg.test(str);
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
  const reg = new RegExp(
    '^(([^<>()\\[\\]\\.,;:\\s@"]+(\\.[^<>()\\[\\]\\.,;:\\s@"]+)*)|(".+"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$'
  );
  return reg.test(email);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === "string" || str instanceof String) {
    return true;
  }
  return false;
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === "undefined") {
    return Object.prototype.toString.call(arg) === "[object Array]";
  }
  return Array.isArray(arg);
}
// mac地址校验
const validateMac = (rule, value, callback) => {
  const regex = /^([A-Fa-f0-9]{2}-){5}[A-Fa-f0-9]{2}$/;
  if (value === "") {
    callback(new Error("请输入mac地址"));
  } else {
    if (value !== "") {
      regex.test(value)
        ? callback()
        : callback(new Error("请输入正确的mac地址"));
    }
    callback();
  }
};
let validateIp = (rule, value, callback) => {
  const regex =
    /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
  const ipv6 = new RegExp(
    "^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$"
  );
  if (value === "") {
    callback(new Error("请输入IP"));
  } else {
    if (value !== "") {
      if (regex.test(value) || ipv6.test(value)) {
        callback();
      } else callback(new Error("请输入正确的IP地址"));
    }
    callback();
  }
};
let validateIpPort = (rule, value, callback) => {
  const regex =
    /^([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\.([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])){3}:([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/;
  // const regexyuming =
  //   /^(([a-zA-Z0-9-]+.){1,}(com|net|edu|miz|biz|cn|cc)):([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/;
  const regexyuming = /^(([a-zA-Z0-9-]+.){1,}(com|net|edu|miz|biz|cn|cc))$/;
  console.log("ip:", regex.test(value));
  console.log("域名:", regexyuming.test(value));
  if (value === "") {
    callback(new Error("请输入IP+端口"));
  } else {
    if (value !== "") {
      if (regex.test(value) || regexyuming.test(value)) {
        callback();
      } else callback(new Error("请输入正确的IP端口或者域名"));
    }
    callback();
  }
};

let validatePort = (rule, value, callback) => {
  const regex =
    /^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/;
  if (value === "") {
    callback(new Error("请输入端口号"));
  } else {
    regex.test(value) ? callback() : callback(new Error("请输入正确的端口号"));
  }
  callback();
};
let validatePhoneNumber = (rule, value, callback) => {
  const phoneNumber = new RegExp(
    "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$"
  );
  if (value === "") {
    callback(new Error("请输入手机号"));
  } else {
    if (phoneNumber.test(value)) {
      callback();
    } else {
      callback(new Error("请输入正确的手机号"));
    }
  }
};
let validatehanzi = (rule, value, callback) => {
  // const hanzi = /[\u4e00-\u9fa5]+/;
  // const regex=/(?!.*[/s])(?!.*[/\u4e00-\u9fa5])^[/w/d]+/
  const regex = /^[\w-]*$/;

  if (value === "") {
    callback(new Error("请输入用户名"));
  } else {
    if (regex.test(value)) {
      callback();
    } else {
      callback(new Error("允许输入数字字母、下划线横杠"));
    }
  }
};
let validateResourceName = (rule, value, callback) => {
  // const hanzi = /[\u4e00-\u9fa5]+/;
  // const regex=/(?!.*[/s])(?!.*[/\u4e00-\u9fa5])^[/w/d]+/
  const regex = /^[\u4e00-\u9fa5\w-]*$/;

  if (value === "") {
    callback(new Error("请输入资源名称"));
  } else {
    if (regex.test(value)) {
      callback();
    } else {
      callback(new Error("允许输入中文数字字母、下划线横杠"));
    }
  }
};

export {
  validateMac,
  validateIp,
  validatePort,
  validatePhoneNumber,
  validatehanzi,
  validateResourceName,
  validateIpPort,
};
